﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Enums;
using mozhi.smarterp.Finance.Treasuries.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Treasuries.Payments
{
    public class PaymentRepository : EfCoreRepository<FinanceDbContext, Payment, Guid>, IPaymentRepository
    {
        public PaymentRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<long> GetCountAsync(string keyword, PaymentType? type, bool? isAdvance, DateTime? startDate, DateTime? endDate, Guid? bankAccountId, Guid? currencyId, Guid? excludeId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var count = await dbContext.Set<Payment>().AsNoTracking()
                .WhereIf(!keyword.IsNullOrEmpty(), x => x.Number.Contains(keyword))
                .WhereIf(type.HasValue, x => x.Type == type)
                .WhereIf(isAdvance.HasValue, x => x.IsAdvanced == isAdvance)
                .WhereIf(startDate.HasValue, x => x.Date >= startDate)
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .WhereIf(bankAccountId.HasValue, x => x.BankAccountId == bankAccountId)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .WhereIf(excludeId.HasValue, x => x.Id != excludeId)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<string> GetLatestNumberAsync()
        {
            var dbContext = await GetDbContextAsync();

            return await dbContext.Set<Payment>().AsNoTracking().MaxAsync(x => x.Number);
        }

        public async Task<List<Payment>> GetListAsync(string keyword, PaymentType? type, bool? isAdvance, DateTime? startDate, DateTime? endDate, Guid? bankAccountId, Guid? currencyId, bool incldueDetials = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = incldueDetials ? (await WithDetailsAsync()).AsNoTracking() : dbContext.Set<Payment>().AsNoTracking();

            var payments = await query
                .WhereIf(!keyword.IsNullOrEmpty(), x => x.Number.Contains(keyword))
                .WhereIf(type.HasValue, x => x.Type == type)
                .WhereIf(isAdvance.HasValue, x => x.IsAdvanced == isAdvance)
                .WhereIf(startDate.HasValue, x => x.Date >= startDate)
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .WhereIf(bankAccountId.HasValue, x => x.BankAccountId == bankAccountId)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .ToListAsync(cancellationToken);

            return payments;
        }

        public async Task<List<Payment>> GetListAsync(List<Guid> ids, bool incldueDetials = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = incldueDetials ? (await WithDetailsAsync()).AsNoTracking() : dbContext.Set<Payment>().AsNoTracking();

            var payments = await query
                .Where(x=>ids.Contains(x.Id))
                .ToListAsync(cancellationToken);

            return payments;
        }

        public async Task<List<Payment>> GetPagedListAsync(string keyword, PaymentType? type, bool? isAdvance, DateTime? startDate, DateTime? endDate, Guid? bankAccountId, Guid? currencyId, int maxResultCount, int sikpCount, string sorting, bool incldueDetials = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = incldueDetials ? (await WithDetailsAsync()).AsNoTracking() : dbContext.Set<Payment>().AsNoTracking();

            var payments = await query
                 .WhereIf(!keyword.IsNullOrEmpty(), x => x.Number.Contains(keyword))
                 .WhereIf(type.HasValue, x => x.Type == type)
                 .WhereIf(isAdvance.HasValue, x => x.IsAdvanced == isAdvance)
                 .WhereIf(startDate.HasValue, x => x.Date >= startDate)
                 .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                 .WhereIf(bankAccountId.HasValue, x => x.BankAccountId == bankAccountId)
                 .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                 .OrderByIf<Payment, IQueryable<Payment>>(!sorting.IsNullOrWhiteSpace(), sorting)
                 .PageBy(sikpCount, maxResultCount)
                 .ToListAsync(cancellationToken);

            return payments;
        }
    }
}
